/*!
 * Currencyr for WordPress - Widget
 * Version 1.0
 *
 * Copyright (c) 2012 Firman Wandayandi
 * Licensed under GNU General Public License Version 2.0 http://www.gnu.org/licenses/gpl-2.0.html
 *
 * @since 1.0
 */

(function ($) {

    $(document).ready(function() {
    
        // Bind "from" param
        $('select[name=from]', '.widget_currencyr .currency-converter').change(function() {
            var $parent = $(this).parents('.currency-converter');
            if ( $('option:selected', this).val() == '' ) $('.currency-amount-code', $parent).html( '&nbsp;' );
            else {
                if ( typeof currencyr_symbols[$('option:selected', this).val()] != 'undefined' )
                    $('.currency-amount-code span.the-data', $parent).html(currencyr_symbols[$('option:selected', this).val()]);
                else
                    $('.currency-amount-code span.the-data', $parent).html($('option:selected', this).val());
            }
        });

        // Hide "to" param
        $('.currency-result-wrapper', '.widget_currencyr .currency-converter').hide();
        
        // Bind "to" param
        $('select[name=to]', '.widget_currencyr .currency-converter').change(function() {
            var $parent = $(this).parents('.currency-converter');
            if ( $('option:selected', this).val() == '' ) {
                $('.currency-result-wrapper', $parent).slideUp( function () {
                    $('.currency-result-code', $parent).html( '&nbsp;' );
                });
            } else {
                if ( typeof currencyr_symbols[$('option:selected', this).val()] != 'undefined' )
                    $('.currency-result-code span.the-data', $parent).html(currencyr_symbols[$('option:selected', this).val()]);
                else
                    $('.currency-result-code span.the-data', $parent).html($('option:selected', this).val());

                $('.currency-result-wrapper', $parent).slideDown();
            }
        });

        // Bind the form submit
        $('.widget_currencyr .currency-converter').submit(function(e) {
            var self = this;
      
            var data = {
                'action': 'currencyr_convert',
                'from'  : $('select[name=from] option:selected', this).val(),
                'to'    : $('select[name=to] option:selected', this).val(),
                'amount': 0
            };

            amount = $( 'input[name=amount]', this ).val();
            if ( !isNaN( parseFloat(amount) ) && isFinite(amount) ) data.amount = parseFloat( amount );

            if ( !data.from || !data.to || !data.amount ) return false;

            if(typeof(currencyr_rates) != 'undefined' && typeof(currencyr_rates[data.from]) != 'undefined' && typeof(currencyr_rates[data.to]) != 'undefined' && typeof(currencyr_settings.base) != 'undefined'){
                //convert locally
                var final_amount = 0;
                if ( data.from == currencyr_settings.base ) {
                    final_amount = data.amount * currencyr_rates[data.to];
                } else {
                    final_amount = data.amount * ( ( 1 / currencyr_rates[data.from] ) * currencyr_rates[data.to] );
                }
                
                var n = final_amount, 
                    c = isNaN(currencyr_settings.precision = Math.abs(currencyr_settings.precision)) ? 2 : currencyr_settings.precision,
                    d = currencyr_settings.decimal == undefined ? "." : currencyr_settings.decimal,
                    t = currencyr_settings.thousand == undefined ? "," : currencyr_settings.thousand,
                    s = n < 0 ? "-" : "", 
                    i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", 
                    j = (j = i.length) > 3 ? j % 3 : 0;
                
                final_amount = s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
            
                $( '.currency-result', self ).val( final_amount );
            }else{
                $.get( currencyr_settings.ajaxurl, data, function( response ) {
                    if ( response != false ) $( '.currency-result', self ).val( response.amount );
                }, 'json' );
            }
            
            return false;
        });

    });

})(jQuery);
